/*
 *  AEDescUtils.h
 *
 *  Copyright (c) 2003 Satimage. All rights reserved.
 *
 * Provides convenient routines to translate C variables into AEDesc's (and v-v).
 */
#ifndef __AEDESCUTILS__
#define __AEDESCUTILS__
#include <Carbon/Carbon.h>
#define typeListOfLongFloat 'Lido'
#define typeListOfShortFloat 'Lido'
#define typeMatrix 'Matr'
#define typePolynomial 'Poly'
#define LidoHeaderSize 16

#ifdef __cplusplus
extern "C" {
#endif

OSStatus FindProcess( OSType inProcessSignature,ProcessSerialNumber *	outPSN);
OSErr RawNotifySmile(AEAddressDesc* smileAddr/*may be NULL*/,const AEDesc* data);	
OSErr NotifySmile(const char* p, Boolean suspend);	
OSErr TryNotifySmile(const AppleEvent* message,const char*p);
	
OSErr AEDescToDouble(const AEDesc d, double* c);
OSErr AEDescToFloat(const AEDesc d, float* c);
OSErr AEDescToShort(const AEDesc d, short* c);
OSErr AEDescToLong(const AEDesc d, long* c);
OSErr AEDescToInt(const AEDesc d, int* c);
OSErr AEDescToUInt(const AEDesc d, unsigned int* c);
OSErr AEDescToBool(const AEDesc d, Boolean* c);
OSErr AEDescToChar(const AEDesc d, char* c);
OSErr AEDescToStr255(const AEDesc d, Str255 c);

OSErr AEDescFromDouble(AEDesc* d, double c);
OSErr AEDescFromFloat(AEDesc* d, float c);
OSErr AEDescFromShort(AEDesc* d, short c);
OSErr AEDescFromLong(AEDesc* d, long c);
OSErr AEDescFromInt(AEDesc* d, int c);
OSErr AEDescFromUInt(AEDesc* d, unsigned int c);
OSErr AEDescFromBool(AEDesc* d, Boolean c);
OSErr AEDescFromChar(AEDesc* d, char c);
OSErr AEDescFromStr255(AEDesc* d,Str255 c);

OSErr AEStreamWriteDouble(AEStreamRef ref, double c);
OSErr AEStreamWriteFloat(AEStreamRef ref, float c);
OSErr AEStreamWriteShort(AEStreamRef ref, short c);
OSErr AEStreamWriteLong(AEStreamRef ref, long c);
OSErr AEStreamWriteInt(AEStreamRef ref, int c);
OSErr AEStreamWriteUInt(AEStreamRef ref, unsigned int c);
OSErr AEStreamWriteBool(AEStreamRef ref, Boolean c);
OSErr AEStreamWriteChar(AEStreamRef ref, char c);
OSErr AEStreamWriteStr255(AEStreamRef ref,Str255 c);

OSErr DoubleArrayFromDesc(const AEDesc* d, long*n,double**p);
OSErr DoubleArrayFromAppleEvent(const AppleEvent* message,DescType key, long*n,double**p);
OSErr DoubleArrayToDesc(AEDesc* d, long n, const double* p);
OSErr DoubleArrayToAppleEvent(AppleEvent* reply,DescType key, long n, const double* p);
OSErr DoubleArrayToAEStream(AEStreamRef  ref, long n, const double* p);

OSErr FloatArrayFromDesc(const AEDesc* d, long*n,float**p);
OSErr FloatArrayFromAppleEvent(const AppleEvent* message,DescType key, long*n,float**p);
OSErr FloatArrayToDesc(AEDesc* d, long n, const float* p);
OSErr FloatArrayToAppleEvent(AppleEvent* reply,DescType key, long n, const float* p);
OSErr FloatArrayToAEStream(AEStreamRef  ref, long n, const float* p);

OSErr LongArrayFromDesc(const AEDesc* d, long*n,long**p);
OSErr LongArrayToDesc(AEDesc* d, long n, const long* p);
OSErr LongArrayToAppleEvent(AppleEvent* reply,DescType key, long n, const long* p);
OSErr LongArrayToAEStream(AEStreamRef  ref, long n, const long* p);

OSErr ShortArrayFromDesc(const AEDesc* d, long*n,short**p);
OSErr ShortArrayToDesc(AEDesc* d, long n,short* p);
OSErr ShortArrayToAppleEvent(AppleEvent* reply,DescType key, long n,short* p);
OSErr ShortArrayToAEStream(AEStreamRef  ref, long n,short* p);

OSErr MatrixFromDesc(const AEDesc* d, long*ncols,long* nrows,double**p);
OSErr MatrixToDesc(AEDesc* d, long ncols,long nrows,const double* p);
OSErr MatrixToAppleEvent(AppleEvent* reply, DescType key, long ncols,long nrows,const double* p);
OSErr MatrixToAEStream(AEStreamRef   ref, long ncols,long nrows,const double* p);

OSErr FloatMatrixFromDesc(const AEDesc* d, long *ncols,long *nrows,float**p);
OSErr FloatMatrixToDesc(AEDesc* d, long ncols,long nrows,const float* p);
OSErr FloatMatrixToAppleEvent(AppleEvent* reply,DescType key, long ncols,long nrows, const float* p);
OSErr FloatMatrixToAEStream(AEStreamRef  ref, long ncols,long nrows, const float* p);

OSErr TypeOfArrayOfReal(const AEDesc* d,DescType* t);
OSErr TypeOfArrayOfRealFromAppleEvent(const AppleEvent* message,DescType key,DescType* t);
OSErr SizeOfArrayOfReal(const AEDesc* d,DescType* t, long* size);
OSErr SizeOfArrayOfRealFromAppleEvent(const AppleEvent* message,DescType key,DescType* t, long* size);


OSErr PutReplyParamError(OSErr err,const AppleEvent* message,AppleEvent* reply,DescType key,DescType desiredType);
void PutReplyStringError(AppleEvent* reply, const char* strErr);

float* double2float(int n,double*pp);
double* float2double(int n,float*pp);
void SWAP16(short* p, int n);
void SWAP32(int* p, int n);
void SWAP64(SInt64* p, int n);

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
OSErr DescDataAs(const AEDesc& d,void* p,long size,DescType t);

inline OSErr operator>>(const AEDesc& source,double& c){return AEDescToDouble(source, &c);}
inline OSErr operator>>(const AEDesc& source,float& c){return AEDescToFloat(source, &c);}
inline OSErr operator>>(const AEDesc& source,short& c){return AEDescToShort(source, &c);}
inline OSErr operator>>(const AEDesc& source,long& c){return AEDescToLong(source, &c);}
inline OSErr operator>>(const AEDesc& source,int& c){return AEDescToInt(source, &c);}
inline OSErr operator>>(const AEDesc& source,unsigned int& c){return AEDescToUInt(source, &c);}
inline OSErr operator>>(const AEDesc& source,Boolean& c){return AEDescToBool(source, &c);}
inline OSErr operator>>(const AEDesc& source,char& c){return AEDescToChar(source, &c);}
inline OSErr operator>>(const AEDesc& source,Str255 c){return AEDescToStr255(source, c);}
inline OSErr operator>>(const AEDesc& data, FSRef& ref){return DescDataAs(data,&ref,sizeof(ref),typeFSRef);}
inline OSErr operator>>(const AEDesc& data, FSSpec& ref){return DescDataAs(data,&ref,sizeof(ref),typeFSS);}

inline OSErr operator<<(AEDesc& d, double c){return AEDescFromDouble(&d, c);}
inline OSErr operator<<(AEDesc& d, float c){return AEDescFromFloat(&d, c);}
inline OSErr operator<<(AEDesc& d, short c){return AEDescFromShort(&d, c);}
inline OSErr operator<<(AEDesc& d, long c){return AEDescFromLong(&d, c);}
inline OSErr operator<<(AEDesc& d, int c){return AEDescFromInt(&d, c);}
inline OSErr operator<<(AEDesc& d, unsigned int c){return AEDescFromUInt(&d, c);}
inline OSErr operator<<(AEDesc& d, Boolean c){return AEDescFromBool(&d, c);}
inline OSErr operator<<(AEDesc& d, char c){return AEDescFromChar(&d, c);}
inline OSErr operator<<(AEDesc& d, Str255 c){return AEDescFromStr255(&d, c);}
inline OSErr operator<< (AEDesc& data, const FSRef& ref){ return AECreateDesc( typeFSRef,&ref,sizeof(ref),&data);}
inline OSErr operator<< (AEDesc& data, const FSSpec& ref){return AECreateDesc( typeFSS,&ref,sizeof(ref),&data);}

inline OSErr AEStreamPut(AEStreamRef& ref, double c){return AEStreamWriteDouble(ref, c);}
inline OSErr AEStreamPut(AEStreamRef& ref, float c){return AEStreamWriteFloat(ref, c);}
inline OSErr AEStreamPut(AEStreamRef& ref, short c){return AEStreamWriteShort(ref, c);}
inline OSErr AEStreamPut(AEStreamRef& ref, long c){return AEStreamWriteLong(ref, c);}
inline OSErr AEStreamPut(AEStreamRef& ref, int c){return AEStreamWriteInt(ref, c);}
inline OSErr AEStreamPut(AEStreamRef& ref, unsigned int c){return AEStreamWriteUInt(ref, c);}
inline OSErr AEStreamPut(AEStreamRef& ref, Boolean c){return AEStreamWriteBool(ref, c);}
inline OSErr AEStreamPut(AEStreamRef& ref, char c){return AEStreamWriteChar(ref, c);}
inline OSErr AEStreamPut(AEStreamRef& ref, Str255 c){return AEStreamWriteStr255(ref, c);}
inline OSErr AEStreamPut(AEStreamRef& ref, const FSRef& fref){ return AEStreamWriteDesc(ref, typeFSRef,&fref,sizeof(fref));}
inline OSErr AEStreamPut(AEStreamRef& ref, const FSSpec& fref){return AEStreamWriteDesc(ref, typeFSS,&fref,sizeof(fref));}
#endif

#endif